﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using EntGuayasSLA.BL.Archivos.Io;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.security;

namespace EntGuayasSLA.BL.Archivos.Pdf
{
    /// <summary>
    /// 
    /// </summary>
    public static class Pdf
    {
        [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
        public static extern bool ZeroMemory(IntPtr Destination, int Length);
        public static string GenerateKey()
        {
            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            // Use the Automatically generated key for Encryption. 
            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }
        /// <summary>
        /// Descriptar file pdf
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dest"></param>
        public static void DecryptFilePdf(String src, String dest, byte[] OWNER)
        {
            try{
                    PdfReader reader = null;
                    try
                    {
                        reader = new PdfReader(src);
                    }
                    catch
                    {

                        reader = new PdfReader(src, OWNER);
                    }
                    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
                    stamper.Close();
                    reader.Close();
                     }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// Descriptar file pdf
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dest"></param>
        public static MemoryStream DecryptFilePdf( String src,byte[] OWNER)
        {
            PdfReader reader = null;
            MemoryStream m = new System.IO.MemoryStream();
            try
            {
                reader = new PdfReader(src);
            }
            catch
            {
                try
                {
                    reader = new PdfReader(src, OWNER);
                }
                catch
                {
                    throw;
                }
               
            }
            PdfStamper stamper = new PdfStamper(reader, m);
            stamper.Close();
            reader.Close();
            return (m);
        }
        /// <summary>
        /// Encriptar pdf
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dest"></param>
        public static void EncryptFilePdf(String src, String dest,byte[] USER, byte[] OWNER)
        {
            try
            {
                if (src == dest) throw new Exception("Input and output must be different");
          
                PdfReader reader = new PdfReader(src);
                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
                stamper.SetEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
                stamper.Close();
                reader.Close();
            }
            catch
            {
                throw;
            }
        }
    }
}